package com.hwadee.property.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hwadee.property.pojo.Community;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface CommunityMapper extends BaseMapper<Community> {

    // 计算总记录数（如果 name 为空，不使用 LIKE）
    @Select("<script>" +
            "SELECT COUNT(*) FROM community" +
            "<where>" +
            "   <if test='name != null and name.trim() != \"\"'> " +
            "       AND community_name LIKE CONCAT('%', #{name}, '%') " +
            "   </if> " +
            "</where>" +
            "</script>")
    int countCommunities(@Param("name") String name);

    // 分页查询（如果 name 为空，不使用 LIKE）
    @Select("<script>" +
            "SELECT * FROM community " +
            "<where>" +
            "   <if test='name != null and name.trim() != \"\"'> " +
            "       AND community_name LIKE CONCAT('%', #{name}, '%') " +
            "   </if> " +
            "</where> " +
            "LIMIT #{size} OFFSET #{offset}" +
            "</script>")
    List<Community> getCommunities(@Param("name") String name, @Param("size") int size, @Param("offset") int offset);

}
